[assembly: WebActivator.PreApplicationStartMethod(typeof(Arms.Presentation.Web.App_Start.NinjectWebCommon), "Start")]
[assembly: WebActivator.ApplicationShutdownMethodAttribute(typeof(Arms.Presentation.Web.App_Start.NinjectWebCommon), "Stop")]

namespace Arms.Presentation.Web.App_Start
{
    using System;
    using System.Web;

    using Microsoft.Web.Infrastructure.DynamicModuleHelper;

    using Ninject;
    using Ninject.Web.Common;
    using System.Security.Principal;
    using Arms.Application.Ninjection;
    using System.Web.Mvc;
    using Arms.Presentation.Web.Common;

    public static class NinjectWebCommon 
    {
        private static readonly Bootstrapper bootstrapper = new Bootstrapper();
        private static IKernel _kernel;

        /// <summary>
        /// Starts the application
        /// </summary>
        public static void Start() 
        {
            DynamicModuleUtility.RegisterModule(typeof(OnePerRequestHttpModule));
            DynamicModuleUtility.RegisterModule(typeof(NinjectHttpModule));
            bootstrapper.Initialize(CreateKernel);
        }
        
        /// <summary>
        /// Stops the application.
        /// </summary>
        public static void Stop()
        {
            bootstrapper.ShutDown();
        }
        
        /// <summary>
        /// Creates the kernel that will manage your application.
        /// </summary>
        /// <returns>The created kernel.</returns>
        private static IKernel CreateKernel()
        {
            _kernel = new StandardKernel();
            _kernel.Settings.AllowNullInjection = true;
            
            GenericIdentity identity = new GenericIdentity("Anonymous");
            GenericPrincipal principal = new GenericPrincipal(identity, new string[] { });

            _kernel.Bind<Func<IKernel>>().ToMethod(ctx => () => new Bootstrapper().Kernel);
            _kernel.Bind<IHttpModule>().To<HttpApplicationInitializationHttpModule>();
            _kernel.Bind<IIdentity>().ToMethod(x => HttpContext.Current.User.Identity);
            _kernel.Bind<IPrincipal>().ToMethod(x => HttpContext.Current.User);
            _kernel.Bind<IFormsService>().To<FormsService>();
            
            RegisterServices();
            ControllerBuilder.Current.SetControllerFactory(new NinjectControllerFactory(_kernel));
            return _kernel;
        }

        /// <summary>
        /// Load your modules or register your services here!
        /// </summary>
        /// <param name="kernel">The kernel.</param>
        private static void RegisterServices()
        {
            _kernel.Load(new CommonModule());
            _kernel.Load(new SecurityModule());
            _kernel.Load(new ProductsModule());
            _kernel.Load(new DiningRoomModule());
            _kernel.Load(new ReportsModule());
            _kernel.Load(new OrdersModule());
            _kernel.Load(new PeopleModule());
        }

        public static T Resolve<T>()
        {
            return (T)_kernel.Get<T>();
        }

        public static IKernel Kernel
        {
            get { return _kernel; }
        }
    }
}
